package com.wan37.logic.fighting.hand;

import com.wan37.logic.domain.vo.AllRoleVO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.fighting.hand.Fighting;
import com.wan37.logic.global.HandlerManager;
import com.wan37.logic.handler.message.CGameMoveHandler;
import com.wan37.logic.listener.EventDispatcher;
import com.wan37.logic.listener.event.EventType;
import com.wan37.logic.listener.event.user.UserSkillMonsterEvent;
import com.wan37.logic.service.cache.*;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author : luoyong
 * @date : 2020-06-28 11:22
 **/
@Component("pVEManager")
public class PVEManager {
    @Autowired
    private RoleServiceCache roleServiceCache;
    @Autowired
    private SceneServiceCache sceneServiceCache;
    @Autowired
    private RouteUtils routeUtils;
    @Autowired
    private UserServiceCache userServiceCache;
    @Autowired
    private SkillServiceCache skillServiceCache;
    @Autowired
    private MonsterServiceCache monsterServiceCache;
    public void responseClient(Map<String, Map<String, Long>> allRoleVONumberMap,
                               int code, String sceneId, List<String> allRoleVOIdList,
                               String skillId, String senderId, int sender, int receiver, MessageVO messageVO) throws Exception {
        if (allRoleVONumberMap != null) {
            routeUtils.sendAttackBroadcastResponse(code, sceneServiceCache.getSceneServiceMap()
                    .get(sceneId).getUserIdSet(), senderId, allRoleVOIdList, skillId, allRoleVONumberMap);
            /**
             * 判断被攻击对象全部是否死亡！
             */
            boolean isGameOver = true;
            int codeDie = CommonFighting.ATTACK_ERROR_BOSS_DIE;
            if (sender == AllRoleLabel.ROLE) {
                if (receiver == AllRoleLabel.MONSTER) {
                    for (AllRoleVO allRoleVO : sceneServiceCache.getSceneServiceMap().get(sceneId).getAllRoleVOMap().values()) {
                        if (allRoleVO.getStatus() == AllRoleLabel.STATUS_LIFE) {
                            isGameOver = false;
                            break;
                        }
                    }
                } else if (receiver == AllRoleLabel.ROLE) {
                    for (String userId : sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet()) {
                        for (String roleId : userServiceCache.getUserServiceMap().get(userId).getRoleIdList()) {
                            if (roleServiceCache.getRoleServiceMap().get(roleId).getRoleVO().getStatus() == AllRoleLabel.STATUS_LIFE) {
                                isGameOver = false;
                                break;
                            }
                            codeDie = CommonFighting.ATTACK_ALL_ROLE_DIE;
                        }
                    }
                }
            } else if (sender == AllRoleLabel.MONSTER) {
                for (String userId : sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet()) {
                    for (String roleId : userServiceCache.getUserServiceMap().get(userId).getRoleIdList()) {
                        if (roleServiceCache.getRoleServiceMap().get(roleId).getRoleVO().getStatus() == AllRoleLabel.STATUS_LIFE) {
                            isGameOver = false;
                            break;
                        }
                        codeDie = CommonFighting.ATTACK_ALL_ROLE_DIE;
                    }
                }
            }
            if (isGameOver) {
                String monsterId = new ArrayList<>(sceneServiceCache.getSceneServiceMap()
                        .get(sceneId).getAllRoleVOMap().keySet()).get(0);
                monsterServiceCache.bossAttackOver(sceneId, monsterId);
                if (codeDie == CommonFighting.ATTACK_ERROR_BOSS_DIE){
                    EventDispatcher.getInstance().fireEvent(new UserSkillMonsterEvent(EventType.SKILL_MONSTER, messageVO.getUserId()));
                    //todo:这里表示你给予了boss最后一击，可以发送奖励,暂时发送了一个消息
                    routeUtils.sendResponse(Common.USER_KILL_BOSS, "你给予了boss最后一击", 1, messageVO.getUserId());
                }
                routeUtils.sendAttackBroadcastResponse(codeDie, sceneServiceCache.getSceneServiceMap()
                        .get(sceneId).getUserIdSet(), senderId, allRoleVOIdList, skillId, allRoleVONumberMap);
                String nextSceneId = sceneServiceCache.getSceneServiceMap().get(sceneId).getPreSceneId();
                Set<String> userIdSet = sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet();
                CGameMoveHandler cGameMoveHandler = (CGameMoveHandler) HandlerManager.getMsgHandler
                        (PtoNum.CGAMEMOVE, messageVO.getUserId(), messageVO.getNetId(), null, messageVO.getCtx());
                for (String userId : userIdSet) {
                    for (String roleId : userServiceCache.getUserServiceMap().get(userId).getRoleIdList()) {
                        RoleVO roleVO = roleServiceCache.getRoleServiceMap().get(roleId).getRoleVO();
                        if (roleVO.getStatus() == AllRoleLabel.STATUS_DIE) {
                            roleVO.getNumberMap().put(CommonBase.MP, (long) 1000);
                            roleVO.setStatus(AllRoleLabel.STATUS_LIFE);
                        }
                    }
                }
                //将场景中的用户返回之前场景，
                //todo 有必要可以进行共同杀怪奖励发送
                for (String userId : userIdSet) {
                    cGameMoveHandler.move(sceneId, nextSceneId, userId,
                            sceneServiceCache.getSceneServiceMap().get(nextSceneId).getSceneDO().getName());
                }
            }
        }
    }
}
